Skip to content

test relative links in example documentation - #1811

Open
KristofersJurevics wants to merge 1 commit into
generative-computing:mainfrom
KristofersJurevics:review/issue-1810
Open

KristofersJurevics wants to merge 1 commit into
generative-computing:mainfrom
KristofersJurevics:review/issue-1810

Conversation

@KristofersJurevics

Copy link
Copy Markdown

fixes issue #1810 by adding a regression test for broken relative links in example documentation. focused validation: 1 test passed; broader documentation checks previously had 5 passed and 19 skipped. Optional project dependencies remain unavailable locally.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@ajbozarth ajbozarth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback from Claude: thanks for picking this up. The main issue is that the test doesn't run in CI yet; details inline. A few process items also need fixing before this can merge:

  • DCO is failing: please amend with git commit -s --amend (and, per AGENTS.md §7, swap the Co-authored-by: Copilot trailer for Assisted-by: GitHub Copilot).
  • The label check needs a conventional-commit PR title, e.g. test: check relative links in example docs.
  • Please fill in the body from .github/pull_request_template.md, and link it with Fixes #1810.
  • Please run pre-commit install and then rerun the hooks; ruff format currently fails on this file.

@@ -0,0 +1,45 @@
"""Check that links in the example documentation point at real files."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI won't collect this file. The pytest step in .github/workflows/quality.yml runs only test and tooling/docs-autogen/test_validate.py, so a test in docs/ only ever runs locally. Could you move it to test/test_example_links.py, next to test/test_example_collection.py? It will then run in CI and pick up the auto-applied unit marker. test/ files also need the SPDX license header; the hook excludes docs/, which is why it passes now. Update EXAMPLES_ROOT / REPOSITORY_ROOT for the new location.

Comment on lines +21 to +26
if line.strip().startswith("```"):
in_code_block = not in_code_block
continue
if in_code_block:
continue
yield from (first or second for first, second in LINK_RE.findall(line))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two parsing cases cause false positives: links inside inline code (`[x](missing.md)` gets flagged) and ~~~ fences (not treated as code blocks).

Suggested change
if line.strip().startswith("```"):
in_code_block = not in_code_block
continue
if in_code_block:
continue
yield from (first or second for first, second in LINK_RE.findall(line))
if line.strip().startswith(("```", "~~~")):
in_code_block = not in_code_block
continue
if in_code_block:
continue
line = re.sub(r"`[^`]*`", "", line)
yield from (first or second for first, second in LINK_RE.findall(line))

parsed = urlsplit(target)
if parsed.scheme or parsed.netloc:
continue
target_path = (markdown_file.parent / parsed.path).resolve()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URL-encoded links like [x](my%20file.md) are reported as missing even when the file exists, so the path should be unquoted first:

Suggested change
target_path = (markdown_file.parent / parsed.path).resolve()
target_path = (markdown_file.parent / unquote(parsed.path)).resolve()


import re
from pathlib import Path
from urllib.parse import urlsplit

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paired with the unquote suggestion below:

Suggested change
from urllib.parse import urlsplit
from urllib.parse import unquote, urlsplit

return missing


def test_example_markdown_links_resolve():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks the current tree, which has no broken links, so nothing shows the check can actually fail. Could you add a couple of tmp_path tests that point _missing_links at a fixture directory (one broken link, one valid link with an anchor, one in a code block)? tooling/docs-autogen/test_validate.py has examples of this pattern. Making _missing_links take the root as a parameter would make that easy.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants